const router = require('koa-router')()
const fs = require('fs')
/*
  公共管理接口
*/
router

// 获取用户列表
.get('/getUserList',async (ctx, next) => {
  const userList = await ctx.db.AdminUsers.find({},'username phone sex date qq weixin name age')
  ctx.body = {
    code: 0,
    message: '用户列表获取成功',
    data: userList
  
  }
})

// 删除用户ById
.get('/deteleUserById', async (ctx, next) => {
  const id = ctx.query.id
  try {
    const product = await ctx.db.AdminUsers.deleteOne({_id: id})
    console.log(product)
    ctx.body = {
      code: 0,
      message: '删除成功'
    }
  } catch (error) {
    console.log(error)
  }
  
})

// 获取用户信息
.get('/getUserInfoById', async (ctx, next) => {
  const id = ctx.query.id
  console.log(id)
  try {
    const product = await ctx.db.AdminUsers.findById(id,'username icon phone sex date qq weixin name age')
    console.log(product)
    ctx.body = {
      code: 0,
      message: '成功获取用户信息',
      data: product
    }
  } catch (error) {
    console.log(error)
    ctx.body = {
      code: 1,
      message: error.message,
    }
  }
  
})

// 更新用户信息
.post('/editUserInfoById', async (ctx, next) => {
  const query = ctx.request.body
  try {
    let product = await ctx.db.AdminUsers.findByIdAndUpdate(query._id, {
      age: query.age,
      name: query.name,
      qq: query.qq,
      weixin: query.weixin,
      sex: query.sex,
      phone: query.phone,
      icon: query.icon
    })
    ctx.body = {
      code: 0,
      message: '用户信息修改成功',
      data: null
    }
  } catch (error) {
    console.log(error)
  }
})


module.exports = router